{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "94e51d4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding = utf-8\n",
    "\n",
    "import tushare as ts\n",
    "\n",
    "import talib\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "import sys\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b259c321",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# code:代码 name:名字，industry:所属行业 area:地区 pe:市盈率 outstanding:流通股本 totals:总股本(万) totalAssets:总资产(万)\n",
    "\n",
    "# liqidAssets:流动资产 fixedAssets:固定资产 reserved:公积金 bvps:每股收益 pd:每股净资 timeToMarket:上市日期\n",
    "\n",
    "def get_stock_list():\n",
    "\n",
    "    df = ts.get_stock_basics()\n",
    "\n",
    "    return df\n",
    "\n",
    "def get_ta(df_code,Dist):\n",
    "\n",
    "    operate_array1 = []\n",
    "\n",
    "    operate_array2 = []\n",
    "\n",
    "    operate_array3 = []\n",
    "\n",
    "    count =0\n",
    "\n",
    "    for code in df_code.index:\n",
    "\n",
    "        # index,0 - 6 date：日期 open：开盘价 high：最高价 close：收盘价 low：最低价 volume：成交量 price_change：价格变动 p_change：涨跌幅\n",
    "\n",
    "        # 7-12 ma5：5日均价 ma10：10日均价 ma20:20日均价 v_ma5:5日均量v_ma10:10日均量 v_ma20:20日均量\n",
    "\n",
    "        df = ts.get_hist_data(code,start='2014-11-20')\n",
    "\n",
    "        dflen = df.shape[0]\n",
    "\n",
    "        count = count +1\n",
    "\n",
    "        ifdflen >35:\n",
    "\n",
    "        (df,operate1) = get_macd(df)\n",
    "\n",
    "        (df,operate2) = get_KDJ(df)\n",
    "\n",
    "        (df,operate3) = Get_RSI(df)\n",
    "\n",
    "        operate_array1.append(operate1)#round(df.iat[(dflen-1),16],2)\n",
    "\n",
    "        operate_array2.append(operate2)\n",
    "\n",
    "        operate_array3.append(operate3)\n",
    "\n",
    "        df_code['MACD']=pd.Series(operate_array1,index=df_code.index)\n",
    "\n",
    "        df_code['KDJ']=pd.Series(operate_array2,index=df_code.index)\n",
    "\n",
    "        df_code['RSI']=pd.Series(operate_array3,index=df_code.index)\n",
    "\n",
    "    returndf_code\n",
    "\n",
    "#通过macd判断买进和买出\n",
    "\n",
    "def get_macd(df):\n",
    "\n",
    "    #参数 12,26,9\n",
    "\n",
    "    macd,macdsignal,macdhist = talib.MACD(df['close'].values,fastperiod=12,slowperiod=26,signalperiod=9)\n",
    "\n",
    "    signal_ma5 = talib.MA(macdsignal,timeperiod=5,matype=0)\n",
    "\n",
    "    signal_ma10 = talib.MA(macdsignal,timeperiod=10,matype=0)\n",
    "\n",
    "    signal_ma20 = talib.MA(macdsignal,timeperiod=20,matype=0)\n",
    "\n",
    "#13-15 DIF DEA DIF-DEA\n",
    "\n",
    "df['macd'] = pd.Series(macd,index=df.index)#DIF\n",
    "\n",
    "df['signal'] = pd.Series(macdsignal,index=df.index)#DEA\n",
    "\n",
    "df['macdhist'] = pd.Series(macdhist,index=df.index)#DIF-DEA\n",
    "\n",
    "dflen = df.shape[0]\n",
    "\n",
    "MAlen =len(signal_ma5)\n",
    "\n",
    "operator =0\n",
    "\n",
    "#俩个数组 1.DIF、DEA均为正，DIF向上穿过DEA\n",
    "\n",
    "#        2.DIF、DEA均为负，DIF向下穿过DEA\n",
    "\n",
    "ifdf.iat[(dflen-1),13] >0:\n",
    "\n",
    "ifdf.iat[(dflen-1),14] >0:\n",
    "\n",
    "ifdf.iat[(dflen-1),13] > df.iat[(dflen-1),14]anddf.iat[(dflen-2),13] <= df.iat[(dflen-2),14]:\n",
    "\n",
    " operator = operator+10#买进\n",
    "\n",
    "else:\n",
    "\n",
    "    ifdf.iat[(dflen-1),14] <0:\n",
    "\n",
    "    ifdf.iat[(dflen-1),13] == df.iat[(dflen-2),14]:\n",
    "\n",
    "operator = operator -10#卖出\n",
    "\n",
    "#DEA与K线发生背离 K线趋势向上，MACD向下，顶背离，将要下降；K线趋势向下，MACD向上，底背离，将要上升\n",
    "\n",
    "ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨\n",
    "\n",
    "ifsignal_ma5[MAlen-1]<=signal_ma10[MAlen-1]andsignal_ma10[MAlen-1]<=signal_ma20[MAlen-1]:#DEA下降\n",
    "\n",
    "operator = operator-1\n",
    "\n",
    "ifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降\n",
    "\n",
    "ifsignal_ma5[MAlen-1]>=signal_ma10[MAlen-1]andsignal_ma10[MAlen-1]>=signal_ma20[MAlen-1]:#DEA上升\n",
    "\n",
    "operator = operator+1\n",
    "\n",
    "#分析MACD柱状图 由负变正将上涨\n",
    "\n",
    "ifdf.iat[(dflen-1),15] >0anddflen >30:\n",
    "\n",
    "foriinrange(1,26):\n",
    "\n",
    "ifdf.iat[(dflen-1-i),15] <=0:\n",
    "\n",
    "operator = operator +5\n",
    "\n",
    "break\n",
    "\n",
    "#由正变负 将降低\n",
    "\n",
    "ifdf.iat[(dflen-1),15] <0anddflen >30:\n",
    "\n",
    "foriinrange(1,26):\n",
    "\n",
    "ifdf.iat[(dflen-1-i),15] >=0:\n",
    "\n",
    "operator = operator -5\n",
    "\n",
    "break\n",
    "\n",
    "returndf,operator\n",
    "\n",
    "#通过KDJ判断买进和卖出\n",
    "\n",
    "defget_KDJ(df):\n",
    "\n",
    "#参数9,3,3\n",
    "\n",
    "slowk,slowd = talib.STOCH(df['high'].values,df['low'].values,df['close'].values,fastk_period=9,slowk_period=3,slowk_matype=0,slowd_period=3,slowd_matype=0)\n",
    "\n",
    "slowkMA5 = talib.MA(slowk,timeperiod=5,matype=0)\n",
    "\n",
    "slowkMA10 = talib.MA(slowk,timeperiod=10,matype=0)\n",
    "\n",
    "slowkMA20 = talib.MA(slowk,timeperiod=20,matype=0)\n",
    "\n",
    "slowdMA5 = talib.MA(slowd,timeperiod=5,matype=0)\n",
    "\n",
    "slowdMA10 = talib.MA(slowd,timeperiod=10,matype=0)\n",
    "\n",
    "slowdMA20 = talib.MA(slowd,timeperiod=20,matype=0)\n",
    "\n",
    "#16,17 K,D\n",
    "\n",
    "df['slowk'] = pd.Series(slowk,index=df.index)#K\n",
    "\n",
    "df['slowd'] = pd.Series(slowd,index=df.index)#D\n",
    "\n",
    "dflen = df.shape[0]\n",
    "\n",
    "MAlen =len(slowdMA5)\n",
    "\n",
    "operator =0\n",
    "\n",
    "#1.K线是快速确认线 -- 数值在90以上为超买信号，数值在10以下为超卖信号；2.D大于80为超卖状态，小于20为超卖状态\n",
    "\n",
    "ifdf.iat[(dflen-1),16] >=90:\n",
    "\n",
    "operator = operator -3\n",
    "\n",
    "ifdf.iat[(dflen-1),16] <=10:\n",
    "\n",
    "operator = operator +3\n",
    "\n",
    "ifdf.iat[(dflen-1),17] >=80:\n",
    "\n",
    "operator = operator -3\n",
    "\n",
    "ifdf.iat[(dflen-1),17] <=20:\n",
    "\n",
    "operator = operator +3\n",
    "\n",
    "#上涨趋势中，K线向上穿过D线，黄金交叉，将进入多头市场，股价将上涨，应该买进\n",
    "\n",
    "ifdf.iat[(dflen-1),16] > df.iat[(dflen-1),17]anddf.iat[(dflen-2),16] <= df.iat[(dflen-2),17]:\n",
    "\n",
    "operator = operator +10\n",
    "\n",
    "#下降趋势中，K线向下穿过D线，死亡交叉，将进入空头市场，股价将下降，应该卖出\n",
    "\n",
    "ifdf.iat[(dflen-1),16] < df.iat[(dflen-1),17]anddf.iat[(dflen-2),16] >= df.iat[(dflen-2),17]:\n",
    "\n",
    "operator = operator -10\n",
    "\n",
    "#3.当随机指标与股价出现背离时，一般为转势的信号。\n",
    "\n",
    "ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨\n",
    "\n",
    "if(slowkMA5[MAlen-1]<=slowkMA10[MAlen-1]andslowkMA10[MAlen-1]<=slowkMA20[MAlen-1])or\\\n",
    "\n",
    "(slowdMA5[MAlen-1]<=slowdMA10[MAlen-1]andslowdMA10[MAlen-1]<=slowdMA20[MAlen-1]):#K,D下降\n",
    "\n",
    "operator = operator -1\n",
    "\n",
    "elifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降\n",
    "\n",
    "if(slowkMA5[MAlen-1]>=slowkMA10[MAlen-1]andslowkMA10[MAlen-1]>=slowkMA20[MAlen-1])or\\\n",
    "\n",
    "(slowdMA5[MAlen-1]>=slowdMA10[MAlen-1]andslowdMA10[MAlen-1]>=slowdMA20[MAlen-1]):#K,D上涨\n",
    "\n",
    "operator = operator +1\n",
    "\n",
    "return(df,operator)\n",
    "\n",
    "#通过RSI判断买入卖出\n",
    "\n",
    "defGet_RSI(df):\n",
    "\n",
    "#参数14,5\n",
    "\n",
    "slowreal = talib.RSI(np.array(df['close']),timeperiod=14)\n",
    "\n",
    "fastreal = talib.RSI(np.array(df['close']),timeperiod=5)\n",
    "\n",
    "slowrealMA5 = talib.MA(slowreal,timeperiod=5,matype=0)\n",
    "\n",
    "slowrealMA10 = talib.MA(slowreal,timeperiod=10,matype=0)\n",
    "\n",
    "slowrealMA20 = talib.MA(slowreal,timeperiod=20,matype=0)\n",
    "\n",
    "fastrealMA5 = talib.MA(fastreal,timeperiod=5,matype=0)\n",
    "\n",
    "fastrealMA10 = talib.MA(fastreal,timeperiod=10,matype=0)\n",
    "\n",
    "fastrealMA20 = talib.MA(fastreal,timeperiod=20,matype=0)\n",
    "\n",
    "#18-19 慢速real，快速real\n",
    "\n",
    "df['slowreal']=pd.Series(slowreal,index=df.index)#慢速real 18\n",
    "\n",
    "df['fastreal']=pd.Series(fastreal,index=df.index)#快速real 19\n",
    "\n",
    "dflen = df.shape[0]\n",
    "\n",
    "MAlen =len(slowrealMA5)\n",
    "\n",
    "operate =0\n",
    "\n",
    "#RSI>80为超买区，RSI<20为超卖区\n",
    "\n",
    "ifdf.iat[(dflen-1),18]>80ordf.iat[(dflen-1),19]>80:\n",
    "\n",
    "operate = operate -2\n",
    "\n",
    "elifdf.iat[(dflen-1),18]<20ordf.iat[(dflen-1),19]<20:\n",
    "\n",
    "operate = operate +2\n",
    "\n",
    "#RSI上穿50分界线为买入信号，下破50分界线为卖出信号\n",
    "\n",
    "if(df.iat[(dflen-2),18]<=50anddf.iat[(dflen-1),18]>50)or(df.iat[(dflen-2),19]<=50anddf.iat[(dflen-1),19]>50):\n",
    "\n",
    "operate = operate +4\n",
    "\n",
    "elif(df.iat[(dflen-2),18]>=50anddf.iat[(dflen-1),18]<50)or(df.iat[(dflen-2),19]>=50anddf.iat[(dflen-1),19]<50):\n",
    "\n",
    "operate = operate -4\n",
    "\n",
    "#RSI掉头向下为卖出讯号，RSI掉头向上为买入信号\n",
    "\n",
    "ifdf.iat[(dflen-1),7]>=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]>=df.iat[(dflen-1),9]:#K线上涨\n",
    "\n",
    "if(slowrealMA5[MAlen-1]<=slowrealMA10[MAlen-1]andslowrealMA10[MAlen-1]<=slowrealMA20[MAlen-1])or\\\n",
    "\n",
    "(fastrealMA5[MAlen-1]<=fastrealMA10[MAlen-1]andfastrealMA10[MAlen-1]<=fastrealMA20[MAlen-1]):#RSI下降\n",
    "\n",
    "operate = operate -1\n",
    "\n",
    "elifdf.iat[(dflen-1),7]<=df.iat[(dflen-1),8]anddf.iat[(dflen-1),8]<=df.iat[(dflen-1),9]:#K线下降\n",
    "\n",
    "if(slowrealMA5[MAlen-1]>=slowrealMA10[MAlen-1]andslowrealMA10[MAlen-1]>=slowrealMA20[MAlen-1])or\\\n",
    "\n",
    "(fastrealMA5[MAlen-1]>=fastrealMA10[MAlen-1]andfastrealMA10[MAlen-1]>=fastrealMA20[MAlen-1]):#RSI上涨\n",
    "\n",
    "operate = operate +1\n",
    "\n",
    "#慢速线与快速线比较观察，若两线同向上，升势较强；若两线同向下，跌势较强；若快速线上穿慢速线为买入信号；若快速线下穿慢速线为卖出信号\n",
    "\n",
    "ifdf.iat[(dflen-1),19]> df.iat[(dflen-1),18]anddf.iat[(dflen-2),19]<=df.iat[(dflen-2),18]:\n",
    "\n",
    "operate = operate +10\n",
    "\n",
    "elifdf.iat[(dflen-1),19]< df.iat[(dflen-1),18]anddf.iat[(dflen-2),19]>=df.iat[(dflen-2),18]:\n",
    "\n",
    "operate = operate -10\n",
    "\n",
    "return(df,operate)\n",
    "\n",
    "defOutput_Csv(df,Dist):\n",
    "\n",
    "TODAY = datetime.date.today()\n",
    "\n",
    "CURRENTDAY=TODAY.strftime('%Y-%m-%d')\n",
    "\n",
    "# reload(sys)\n",
    "\n",
    "sys.setdefaultencoding(\"gbk\")\n",
    "\n",
    "df.to_csv(Dist+CURRENTDAY+'stock.csv',encoding='gbk')#选择保存\n",
    "\n",
    "df = get_stock_list()\n",
    "\n",
    "Dist ='data/TEST'\n",
    "\n",
    "df = get_ta(df,Dist)\n",
    "\n",
    "Output_Csv(df,Dist)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
